package kulstats;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.google.appengine.api.datastore.Key;


public class ActivityController {
	private static final ActivityController instance = new ActivityController();

	private ActivityController(){
	}
	
	public static ActivityController getInstance(){
		return instance;
	}
	
	public void saveActivity(String trackType,long trackStart,int trackTime, int trackPaused ,int trackPauses, 
			String trackCourse,String trackLocation, String trackSpareType) {
		Date startDate = new Date(trackStart);
		Date stopDate  = new Date(trackStart + (long) trackTime * 60 * 1000);
		
		// deze gegevens moeten nog aangevuld worden
		int points = 0;
		List<Question> questionnaire = null;
		Course course = Registry.courseManager().getCourseByID(trackCourse);
		Activity act = null;
		
		switch(ActivityType.valueOf(trackType.toUpperCase()))
		{
			case STUDY:
				act = new Study(startDate,stopDate,points,questionnaire,course,trackPaused,trackPauses,null);
				break;
			case LESSON:
				act = new Lesson(startDate,stopDate,points,questionnaire,course,LessonType.LECTURE);
				break;
			case SPARE_TIME:
				act = new SpareTime(startDate,stopDate,questionnaire, SpareTimeType.valueOf(trackSpareType.toUpperCase()));
				break;
		}
		
		//de Key van de user ophalen
		User currentUser = Registry.userManager().getCurrentUser();
		Key key = currentUser.getKey();
		Registry.activityManager().saveActivity(act,key);
	}
	
	public boolean startActivity(String trackType, long trackStart, String trackCourse, String trackLessonType, String trackLocation, String trackSpareType) {
		Date startDate = new Date(trackStart);
		List<Question> questionnaire = null;
		Course course = Registry.courseManager().getCourseByID(trackCourse);

		Activity act = null;
		
		switch(ActivityType.valueOf(trackType.toUpperCase()))
		{
			case STUDY:
				questionnaire = Registry.questionnaireManager().manageQuestions(ActivityType.valueOf(trackType.toUpperCase()), null, null);
				act = new Study(startDate,null,0,questionnaire,course,0,0,null);
				break;
			case LESSON:
				questionnaire = Registry.questionnaireManager().manageQuestions(ActivityType.valueOf(trackType.toUpperCase()), null, LessonType.valueOf(trackLessonType.toUpperCase()));
				act = new Lesson(startDate, null, 0, questionnaire, course, LessonType.valueOf(trackLessonType.toUpperCase()));
				break;
			case SPARE_TIME:
				questionnaire = Registry.questionnaireManager().manageQuestions(ActivityType.valueOf(trackType.toUpperCase()), SpareTimeType.valueOf(trackSpareType.toUpperCase()), null);
				act = new SpareTime(startDate,null,questionnaire, SpareTimeType.valueOf(trackSpareType.toUpperCase()));
				break;
		}
		
		User currentUser = Registry.userManager().getCurrentUser();
		Key key = currentUser.getKey();
		Registry.activityManager().saveActivity(act,key);
		
		return true;
	}
	
	public boolean stopActivity(Date stopDate) {

		Activity curAct = Registry.activityManager().getLastActivity(Registry.userManager().getCurrentUser());
		if(curAct != null && curAct.getStopDate() == null) {
			curAct.setStopDate(stopDate);
			//((Study)curAct).setPoints(PointsCalculator.calculatePoints(curAct));
			Registry.activityManager().updateActivity(curAct);
			return true;
		}
		return false;
	}
	
	public boolean startPause(Date pauseDate) {
		Activity curAct = Registry.activityManager().getLastActivity(Registry.userManager().getCurrentUser());
		if(curAct != null && curAct.getStopDate() == null) {
			if(curAct.getAType() == ActivityType.STUDY) {
				((Study)curAct).setLastPauseDate(pauseDate);
				Registry.activityManager().updateActivity(curAct);
				return true;
			}
		}
		return false;
	}
	
	public boolean stopPause(Date pauseDate) {
		Activity act = Registry.activityManager().getLastActivity(Registry.userManager().getCurrentUser());
		if(act != null && act.getStopDate() == null) {
			if(act.getAType() == ActivityType.STUDY) {
				Study curAct = (Study)act;
				Date lastPauseDate = curAct.getLastPauseDate();
				if(lastPauseDate == null)
					return false;
				long differenceInMinutes = (pauseDate.getTime() - lastPauseDate.getTime() + 30000)/60000;
				curAct.setLastPauseDate(null);
				if(differenceInMinutes > 0) {
					curAct.setPaused(curAct.getPaused()+((Long)differenceInMinutes).intValue());
					curAct.setPauses(curAct.getPauses()+1);
				}
				Registry.activityManager().updateActivity(curAct);
				return true;
			}
		}
		return false;
	}
	
	public LinkedHashMap<Question,List<String>> getQuestions() {
		Activity lastAct = Registry.activityManager().getLastActivity(Registry.userManager().getCurrentUser());
		System.out.println(lastAct);
		LinkedHashMap<Question,List<String>> toReturn = new LinkedHashMap<Question,List<String>>();
		if(lastAct == null) {
			return toReturn;
		}
		if(lastAct.getStopDate() == null) {
			return toReturn;
		}
		/*if(lastAct.getQuestionnaire() != null) {
			return toReturn;
		}*/
		List<Question> questions = null;
		switch(lastAct.getAType())
		{
			case STUDY:
				questions = Registry.questionnaireManager().manageQuestions(ActivityType.STUDY, null, null);
				break;
			case LESSON:
				questions = Registry.questionnaireManager().manageQuestions(ActivityType.LESSON, null, ((Lesson)lastAct).getLessontype());
				break;
			case SPARE_TIME:
				questions = Registry.questionnaireManager().manageQuestions(ActivityType.SPARE_TIME, ((SpareTime)lastAct).getSpareTimeType(), null);
				break;
		}
		System.out.println(questions);
		
		for(Question q : questions) {
			toReturn.put(q, Registry.questionnaireManager().getAnswerList(q.getAnswerlist()));
			System.out.println(q.getAnswerlist() + ":" + Registry.questionnaireManager().getAnswerList(q.getAnswerlist()));
		}
		
		return toReturn;
	}
	
	public boolean saveQuestions(Map<Integer, String> answers) {
		System.out.println(answers);
		int successes = 0;
		Activity curAct = Registry.activityManager().getLastActivity(Registry.userManager().getCurrentUser());
		List<Question> questions = curAct.getQuestionnaire();
		for(Question q : questions) {
			for(Integer i : answers.keySet()) {
				if(q.getQuestionID() == i) {
					q.setAnswer(answers.get(i));
					successes++;
				}
			}
		}
		curAct.setQuestionnaire(questions);
		
		Registry.activityManager().updateActivity(curAct);
		return (successes == questions.size());
	}
	
	/*public boolean skipQuestions() {
		Activity curAct = activityManager.getLastActivity(userManager.getCurrentUser());
		if(!activityManager.isAnswered(curAct)) {
			curAct.setQuestionnaire(null);
			return true;
		}
		return false;
	}*/
}










